package com.lanou.tipsy.fragment.singer.detail;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.singer.SimilarSingersAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.singer.singers_info.SInfoData;
import com.lanou.tipsy.entity.singer.singers_info.SInfoRoot;
import com.lanou.tipsy.entity.singer.singers_similar.SSimilarData;
import com.lanou.tipsy.entity.singer.singers_similar.SSimilarRoot;
import com.lanou.tipsy.tools.listview.NoScrollGridView;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/22.
 * 歌手-歌手详情-歌手信息
 */
public class SDetailInfosFragment extends BaseFragment {
    public static final String TAG = "SDetailInfosFragment";
    private ArrayList<SSimilarData> datas;
    private NoScrollGridView gridView;
    private SimilarSingersAdapter adapter;
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    private int info_id = 50530;
    private StringRequest request;
    private Context context;
    private SInfoData data;
    private TextView tv_singer_info, tv_personal_life, tv_pride_recode, tv_entertainment;
    private TextView tv_singer_chinese_name, tv_singer_other_name, tv_singer_english_name, tv_singer_birthday,
            tv_singer_country, tv_singer_nation, tv_singer_birth_place, tv_singer_height, tv_singer_weight,
            tv_singer_blood_type, tv_singer_school, tv_singer_agent, tv_singer_genre, tv_singer_profession;

    public SDetailInfosFragment() {
        super();
    }

    public SDetailInfosFragment(int urlId) {
        this();
        this.info_id = urlId;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer_det_infos, null);
        initView(view);
        analysis();
        return view;
    }

    //初始化控件
    private void initView(View view) {
        gridView = (NoScrollGridView) view.findViewById(R.id.gv_similars_singers);
        volleySingleton = VolleySingleton.getInstance();
        errorListener = new ResponseErrorListener();
        tv_entertainment = (TextView) view.findViewById(R.id.tv_entertainment);
        tv_personal_life = (TextView) view.findViewById(R.id.tv_personal_life);
        tv_pride_recode = (TextView) view.findViewById(R.id.tv_pride_recode);
        tv_singer_agent = (TextView) view.findViewById(R.id.tv_singer_agent);
        tv_singer_birth_place = (TextView) view.findViewById(R.id.tv_singer_birth_place);
        tv_singer_info = (TextView) view.findViewById(R.id.tv_singer_info);
        tv_singer_birthday = (TextView) view.findViewById(R.id.tv_singer_birthday);
        tv_singer_blood_type = (TextView) view.findViewById(R.id.tv_singer_blood_type);
        tv_singer_chinese_name = (TextView) view.findViewById(R.id.tv_singer_chinese_name);
        tv_singer_country = (TextView) view.findViewById(R.id.tv_singer_country);
        tv_singer_english_name = (TextView) view.findViewById(R.id.tv_singer_english_name);
        tv_singer_genre = (TextView) view.findViewById(R.id.tv_singer_genre);
        tv_singer_weight = (TextView) view.findViewById(R.id.tv_singer_weight);
        tv_singer_school = (TextView) view.findViewById(R.id.tv_singer_school);
        tv_singer_profession = (TextView) view.findViewById(R.id.tv_singer_profession);
        tv_singer_other_name = (TextView) view.findViewById(R.id.tv_singer_other_name);
        tv_singer_nation = (TextView) view.findViewById(R.id.tv_singer_nation);
        tv_singer_height = (TextView) view.findViewById(R.id.tv_singer_height);
    }

    private void analysis() {
        String similar_singer = Constants.SINGER_INFO_SIMILAR_START_URL + info_id +
                Constants.SINGER_INFO_SIMILAR_END_URL;
        //相似歌手解析
        request = new StringRequest(similar_singer, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                SSimilarRoot root = JSON.parseObject(response, SSimilarRoot.class);
                datas = root.getData();
                adapter = new SimilarSingersAdapter(datas, context);
                gridView.setAdapter(adapter);
            }
        }, errorListener);
        volleySingleton.addQueue(request);
        // 歌手信息解析
        String singer_info_url = Constants.SINGER_INFO_START + info_id +
                Constants.SINGER_INFO_END;
        request = new StringRequest(singer_info_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                SInfoRoot root = JSON.parseObject(response, SInfoRoot.class);
                data = root.getData();
                if (data.getEntertainmentExperience() != null && data.getEntertainmentExperience() != "") {
                    tv_entertainment.setText(data.getEntertainmentExperience());
                } else {
                    tv_entertainment.setVisibility(View.GONE);
                }
                tv_personal_life.setText(data.getPersonalLife());
                tv_pride_recode.setText(data.getAwards());
                tv_singer_info.setText(data.getBrief());
                tv_singer_height.setText(data.getHeight());
                tv_singer_nation.setText(data.getNation());
                if (data.getEnglishName() != null && data.getEnglishName() != "") {
                    tv_singer_english_name.setText(data.getEnglishName());
                } else {
                    tv_singer_english_name.setVisibility(View.GONE);
                }
                tv_singer_other_name.setText(data.getAlias().get(0));
                tv_singer_profession.setText(data.getCareer());
                tv_singer_school.setText(data.getSchool());
                tv_singer_genre.setVisibility(View.GONE);
                tv_singer_weight.setText(data.getWeight());
                tv_singer_country.setText(data.getCountry());
                tv_singer_blood_type.setText(data.getBloodType());
                tv_singer_chinese_name.setText(data.getChineseName());
                tv_singer_birthday.setText(data.getBirthDate());
                tv_singer_birth_place.setText(data.getBirthPlace());
                tv_singer_agent.setText(data.getCompanyName());
            }
        }, errorListener);
        volleySingleton.addQueue(request);
    }


}
